package com.hrm.company.controller;

import com.hrm.company.service.CompanyService;
import com.hrm.domain.company.Company;
import com.hrm.common.entity.Result;
import com.hrm.common.entity.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 林海湾Sea
 * @Date :  2021/11/19 19:58
 * @Decription: 企业接口（控制器）
 */

@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    /**
     * 添加企业
     * @param company
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result add(@RequestBody Company company) {
        companyService.add(company);
        return Result.SUCCESS();
    }

    /**
     * 根据id更新企业信息
     * @param id
     * @param company
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String id, @RequestBody Company company) {
        Company companyInfo = companyService.findById(id);
        companyInfo.setName(company.getName());
        companyInfo.setRemarks(company.getRemarks());
        companyInfo.setState(company.getState());
        companyInfo.setAuditState(company.getAuditState());
        companyService.update(company);
        return Result.SUCCESS();
    }

    /**
     * 根据id删除企业信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String id) {
        companyService.deleteById(id);
        return Result.SUCCESS();
    }

    /**
     * 根据id获取企业信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findCompanyById(@PathVariable(value = "id") String id) {
        Company companyInfo = companyService.findById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(companyInfo);
        return result;
    }

    /**
     * 获取全部企业--企业列表
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Result findAll() {
        List<Company> companyList = companyService.findAll();
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(companyList);
        return result;
    }

}
